/**
 * Created by lx on 2018/4/3.
 */
//@ sourceURL=moneyStatic.js 
CommonUtils.regNamespace("com.im.moneyPlaneManage.moneyStatic");

$(function() {
    com.im.moneyPlaneManage.moneyStatic.invoke.init();
});

com.im.moneyPlaneManage.moneyStatic.invoke = function() {
    var client = new com.im.common.ajax();

    /**
     * 页面初始化
     * @private
     */
    function _init() {
        //初始化时间、复选框、下拉框等事件和样式
        _initCommpentAction();
        //资金计划列表查询
        $("#vQueryMoneys").on("click", function () {
            _queryMoneys(1);
        }).trigger("click");
        //条件重置
        $("#vResetCond").on("click", _resetCond);
        //导出
        $('#exportProjectPlanBtn').click(function (){
        	_exportList();
        });
        $('#printProjectPlanBtn').click(function (){
      	  CreateFormPage("采购计划", $('#vMoneyTable'))
      });
    }

    function _initCommpentAction() {
        //时间插件
        $('#startDatepicker,#endDatepicker').datepicker({
        	startView: 'decade',  
            minView: 'decade',  
            format: 'yyyy',  
            maxViewMode: 2,  
            minViewMode:2,  
            autoclose: true 
        });
        //复选框
        $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
            checkboxClass: 'icheckbox_minimal-blue',
            radioClass   : 'iradio_minimal-blue'
        });
        //下拉框
        $('.select2').select2();

        //查询-建设单位to实施项目
        $("#searchOrgName").project2unit2itemCommpent({
            elmIdPlanTypeSel : "",
            elmIdUnitSel : "searchOrgName",//组织单位下拉元素
            elmIdItemSel : "vSubjectName",//实施项目下拉元素
            orgType : "10",//初始化单位时，单位类型
            callbackFunc: function (callBackType, ret) {//下拉框值改变时回调
            	_queryMoneys(1);
            }
        });
    }
    //导出表格
    function _exportList(){
    	var param = {
    		    "classes": "FundPlanService",
    		    "fileName": "资金计划统计列表",
    		    "method": "queryFundPlanStaticList",
    		    "title": {
                    "impleProjName":"实施项目名称",
                    "startDt": "开工年$建设年限",
                    "endDt": "完工年$建设年限",
                    "roughTotalInvest.total": "合计$概算总投资",
                    "roughTotalInvest.fundBu": "部补助$概算总投资",
                    "roughTotalInvest.fundSheng": "省财政$概算总投资",
                    "roughTotalInvest.fundGao": "政府融资$概算总投资",
                    "roughTotalInvest.fundShi": "市州配套$概算总投资",
                    "roughTotalInvest.fundQi": "企业自筹$概算总投资",
                    "roughTotalInvest.fundOther": "企业自筹$概算总投资",

                    "totalIssued.total": "合计$累计至"+((new Date).getFullYear()-1)+"年下达的计划",
                    "totalIssued.fundBu": "部补助$累计至"+((new Date).getFullYear()-1)+"年下达的计划",
                    "totalIssued.fundSheng": "省财政$累计至"+((new Date).getFullYear()-1)+"年下达的计划",
                    "totalIssued.fundGao": "政府融资$累计至"+((new Date).getFullYear()-1)+"年下达的计划",
                    "totalIssued.fundShi": "市州配套$累计至"+((new Date).getFullYear()-1)+"年下达的计划",
                    "totalIssued.fundQi": "企业自筹$累计至"+((new Date).getFullYear()-1)+"年下达的计划",
                    "totalIssued.fundOther": "其它$累计至"+((new Date).getFullYear()-1)+"年下达的计划",

                    "totalInvest.total": "合计$累计至"+((new Date).getFullYear()-1)+"年完成的投资",
                    "totalInvest.fundBu": "部补助$累计至"+((new Date).getFullYear()-1)+"年完成的投资",
                    "totalInvest.fundSheng": "省财政$累计至"+((new Date).getFullYear()-1)+"年完成的投资",
                    "totalInvest.fundGao": "政府融资$累计至"+((new Date).getFullYear()-1)+"年完成的投资",
                    "totalInvest.fundShi": "市州配套$累计至"+((new Date).getFullYear()-1)+"年完成的投资",
                    "totalInvest.fundQi": "企业自筹$累计至"+((new Date).getFullYear()-1)+"年完成的投资",
                    "totalInvest.fundOther": "其它$累计至"+((new Date).getFullYear()-1)+"年完成的投资",

                    "totalFund.total": "合计$累计至"+((new Date).getFullYear()-1)+"年到位资金",
                    "totalFund.fundBu": "部补助$累计至"+((new Date).getFullYear()-1)+"年到位资金",
                    "totalFund.fundSheng": "省财政$累计至"+((new Date).getFullYear()-1)+"年到位资金",
                    "totalFund.fundGao": "政府融资$累计至"+((new Date).getFullYear()-1)+"年到位资金",
                    "totalFund.fundShi": "市州配套$累计至"+((new Date).getFullYear()-1)+"年到位资金",
                    "totalFund.fundQi": "企业自筹$累计至"+((new Date).getFullYear()-1)+"年到位资金",
                    "totalFund.fundOther": "其它$累计至"+((new Date).getFullYear()-1)+"年到位资金",

                    "investPlan.total": "合计$"+(new Date).getFullYear()+"年投资计划",
                    "investPlan.fundBu": "部补助$"+(new Date).getFullYear()+"年投资计划划",
                    "investPlan.fundSheng": "省财政$"+(new Date).getFullYear()+"年投资计划",
                    "investPlan.fundGao": "政府融资$"+(new Date).getFullYear()+"年投资计划",
                    "investPlan.fundShi": "市州配套$"+(new Date).getFullYear()+"年投资计划",
                    "investPlan.fundQi": "企业自筹$"+(new Date).getFullYear()+"年投资计划",
                    "investPlan.fundOther": "其它$"+(new Date).getFullYear()+"年投资计划",
    		    }
    		};
    	param.param={};
        param.impleProjId =  $("#vSubjectName option:selected").attr("value");
        param.impleProjName =$("#vSubjectName option:selected").html();
        param.developOrg =  $("#searchOrgName option:selected").attr("value");
    	param.param.startDatepicker = $("#startDatepicker").val();
    	param.param.endDatepicker = $("#endDatepicker").val();
    	var url='/InfoManageWeb/commonService/export?json='+encodeURI(JSON.stringify(param));
    	window.open(url, '_blank');
    }
    /**
     * 条件重置
     * @private
     */
    function _resetCond() {
        $('#searchOrgName,#vSubjectName,#startDatepicker,#endDatepicker').val('');
        $('#searchOrgName').val("").trigger("change");
        $('#vSubjectName').val("").trigger("change");
        _queryMoneys(1);
    }

    function _queryMoneys(index) {
        if(com.im.moneyPlaneManage.moneyStatic.moneyColumns == undefined) {
            com.im.moneyPlaneManage.moneyStatic.moneyColumns = [[
                {field:'序号',title:"序号",fitColumns:true,rowspan:2,
                    formatter: function(value,row,index){
                        return  index+1;
                    }
                },
                {field:'impleProjName',title:"实施项目名称",fitColumns:true,rowspan:2,align:'center'},
                {field:'developOrgName',title:"建设单位",fitColumns:true,rowspan:2,align:'center',width:$(this).width()*0.05,fitColumns:true,
                    formatter: function(value,row,index){
                        return '<span  title='+value+'>'+value+'</span>';
                    }
                },
                {field:'date',colspan:2,title:'建设年份',align:'center'},
                {field:'preWorkSituation',title:"前期工作情况",fitColumns:true,rowspan:2,align:'center',width:$(this).width()*0.06,fitColumns:true,
                    formatter: function(value,row,index){
                        return '<span  title='+value+'>'+value+'</span>';
                    }
                },
                {field:'roughTotalInvest',colspan:7,title:'概算总投资（万元）',align:'center'},
                {field:'totalIssued',colspan:7,title:'累计至'+((new Date).getFullYear()-1)+'年下达的计划（万元）',align:'center'},
                {field:'totalInvest',colspan:7,title:'累计至'+((new Date).getFullYear()-1)+'年完成的投资（万元）',align:'center'},
                {field:'totalFund',colspan:7,title:'累计至'+((new Date).getFullYear()-1)+'年到位资金（万元）',align:'center'},
                {field:'investPlan',colspan:7,title:(new Date).getFullYear()+'年投资计划（万元）',align:'center'},
                //{field:'statusCd',title:"状态",fitColumns:true,rowspan:2}
            ],
                [{field:'startDt',title:"开工年",fitColumns:true},
                    {field:'endDt',title:"完工年",fitColumns:true},

                    {field:'roughTotalInvest.total',title:"合计",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.roughTotalInvest.total;
                        }
                    },
                    {field:'roughTotalInvest.fundBu',title:"部补助",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.roughTotalInvest.fundBu;
                        }
                    },
                    {field:'roughTotalInvest.fundSheng',title:"省财政",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.roughTotalInvest.fundSheng;
                        }
                    },
                    {field:'roughTotalInvest.fundGao',title:"政府融资",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.roughTotalInvest.fundGao;
                        }
                    },
                    {field:'roughTotalInvest.fundShi',title:"市州配套",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.roughTotalInvest.fundShi;
                        }
                    },
                    {field:'roughTotalInvest.fundQi',title:"企业自筹",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.roughTotalInvest.fundQi;
                        }
                    },
                    {field:'roughTotalInvest.fundOther',title:"其它",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.roughTotalInvest.fundOther;
                        }
                    },

                    {field:'totalIssued.total',title:"合计",fitColumns:true,
                        formatter: function(value,row,index){
                            return  '<a href="javascript:;" onclick="com.im.moneyPlaneManage.moneyStatic.invoke.moreList('+index+',\'totalIssued\')">'+row.totalIssued.total+'</a>';
                        }
                    },
                    {field:'totalIssued.fundBu',title:"部补助",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.totalIssued.fundBu;
                        }
                    },
                    {field:'totalIssued.fundSheng',title:"省财政",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.totalIssued.fundSheng;
                        }
                    },
                    {field:'totalIssued.fundGao',title:"政府融资",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.totalIssued.fundGao;
                        }
                    },
                    {field:'totalIssued.fundShi',title:"市州配套",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.totalIssued.fundShi;
                        }
                    },
                    {field:'totalIssued.fundQi',title:"企业自筹",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.totalIssued.fundQi;
                        }
                    },
                    {field:'totalIssued.fundOther',title:"其它",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.totalIssued.fundOther;
                        }
                    },

                    {field:'totalInvest.total',title:"合计",fitColumns:true,
                        formatter: function(value,row,index){
                            return  '<a href="javascript:;" onclick="com.im.moneyPlaneManage.moneyStatic.invoke.moreList('+index+',\'totalInvest\')">'+row.totalInvest.total+'</a>';
                        }
                    },
                    {field:'totalInvest.fundBu',title:"部补助",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.totalInvest.fundBu;
                        }
                    },
                    {field:'totalInvest.fundSheng',title:"省财政",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.totalInvest.fundSheng;
                        }
                    },
                    {field:'totalInvest.fundGao',title:"政府融资",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.totalInvest.fundGao;
                        }
                    },
                    {field:'totalInvest.fundShi',title:"市州配套",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.totalInvest.fundShi;
                        }
                    },
                    {field:'totalInvest.fundQi',title:"企业自筹",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.totalInvest.fundQi;
                        }
                    },
                    {field:'totalInvest.fundOther',title:"其它",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.totalInvest.fundOther;
                        }
                    },

                    {field:'totalFund.total',title:"合计",fitColumns:true,
                        formatter: function(value,row,index){
                            return  '<a href="javascript:;" onclick="com.im.moneyPlaneManage.moneyStatic.invoke.moreList('+index+',\'totalFund\')">'+row.totalFund.total+'</a>';
                        }
                    },
                    {field:'totalFund.fundBu',title:"部补助",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.totalFund.fundBu;
                        }
                    },
                    {field:'totalFund.fundSheng',title:"省财政",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.totalFund.fundSheng;
                        }
                    },
                    {field:'totalFund.fundGao',title:"政府融资",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.totalFund.fundGao;
                        }
                    },
                    {field:'totalFund.fundShi',title:"市州配套",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.totalFund.fundShi;
                        }
                    },
                    {field:'totalFund.fundQi',title:"企业自筹",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.totalFund.fundQi;
                        }
                    },
                    {field:'totalFund.fundOther',title:"其它",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.totalFund.fundOther;
                        }
                    },

                    {field:'investPlan.total',title:"合计",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.investPlan.total;
                        }
                    },
                    {field:'investPlan.fundBu',title:"部补助",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.investPlan.fundBu;
                        }
                    },
                    {field:'investPlan.fundSheng',title:"省财政",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.investPlan.fundSheng;
                        }
                    },
                    {field:'investPlan.fundGao',title:"政府融资",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.investPlan.fundGao;
                        }
                    },
                    {field:'investPlan.fundShi',title:"市州配套",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.investPlan.fundShi;
                        }
                    },
                    {field:'investPlan.fundQi',title:"企业自筹",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.investPlan.fundQi;
                        }
                    },
                    {field:'investPlan.fundOther',title:"其它",fitColumns:true,
                        formatter: function(value,row,index){
                            return  row.investPlan.fundOther;
                        }
                    },]
            ];
        }

        var req = {};
        req.pageInfo = {};
        req.pageInfo.pageNumber = index;
        req.pageInfo.pageSize = 20;
        req.impleProjId =  $("#vSubjectName option:selected").attr("value");
        req.impleProjName =  req.impleProjId == '' ? '' : $("#vSubjectName option:selected").html();
        req.developOrg = $("#searchOrgName option:selected").attr("value");
        req.startDt = $("#startDatepicker").val();
        req.endDt = $("#endDatepicker").val();
        req.more = 1;
        var resp = client.callAsJson("fundPlanService/queryFundPlanStaticList", req);
        if (!resp.success) {
            toastr.error(resp.resultMsg);
            return;
        }

        $("#vMoneyTable").empty();
        $("#vMoneyTable").datagrid({
            pagination:true,
            columns:com.im.moneyPlaneManage.moneyStatic.moneyColumns,
            data:resp.result
        });
        var p = $('#vMoneyTable').datagrid('getPager');
        $(p).pagination({
            pageSize: 20,
            pageList: [10,20,30],
            beforePageText: '第',
            afterPageText: '页    共 {pages}  页',
            displayMsg: '当前显示 {from} - {to} 条记录 共 {total}  条记录',
            onSelectPage:function(pageNumber, pageSize){
                _queryMoneys(pageNumber);
                param.pageInfo.pageNumber = pageNumber;
                param.pageInfo.pageSize = pageSize;
                // param.pageSize=pageSize;
                var impleProjId = $("#vSubjectName option:selected").attr("value");
                var orgId =  $("#searchOrgName option:selected").attr("value");
                var startDt = $("#startDatepicker").val();
                var endDt = $("#endDatepicker").val();
                param.startDt = startDt;
                param.endDt = endDt;
                param.impleProjId = impleProjId;
                param.developOrg = orgId;
                param.more = 1;
                $('#vMoneyTable').datagrid('reload',param);
            }
        });
    }

    function CreateFormPage(strPrintName, printDatagrid) {
      	 var tableString = '<table cellspacing="0" class="pb" style="TABLE-LAYOUT: fixed">';
      	 var frozenColumns = printDatagrid.datagrid("options").frozenColumns;  // 得到frozenColumns对象
      	 var columns = printDatagrid.datagrid("options").columns;    // 得到columns对象
      	 var nameList = '';
      	
      	 // 载入title
      	 if (typeof columns != 'undefined' && columns != '') {
      	     $(columns).each(function (index) {
      	         tableString += '\n<tr>';
      	         if (typeof frozenColumns != 'undefined' && typeof frozenColumns[index] != 'undefined') {
      	             for (var i = 0; i < frozenColumns[index].length; ++i) {
      	                 if (!frozenColumns[index][i].hidden) {
      	                     tableString += '\n<th width="' + frozenColumns[index][i].width + '"';
      	                     if (typeof frozenColumns[index][i].rowspan != 'undefined' && frozenColumns[index][i].rowspan > 1) {
      	                         tableString += ' rowspan="' + frozenColumns[index][i].rowspan + '"';
      	                     }
      	                     if (typeof frozenColumns[index][i].colspan != 'undefined' && frozenColumns[index][i].colspan > 1) {
      	                         tableString += ' colspan="' + frozenColumns[index][i].colspan + '"';
      	                     }
      	                     if (typeof frozenColumns[index][i].field != 'undefined' && frozenColumns[index][i].field != '') {
      	                         nameList += ',{"f":"' + frozenColumns[index][i].field + '", "a":"' + frozenColumns[index][i].align + '"}';
      	                     }
      	                     tableString += '>' + frozenColumns[0][i].title + '</th>';
      	                 }
      	             }
      	         }
      	         for (var i = 0; i < columns[index].length; ++i) {
      	             if (!columns[index][i].hidden) {
      	                 tableString += '\n<th width="' + columns[index][i].width + '"';
      	                 if (typeof columns[index][i].rowspan != 'undefined' && columns[index][i].rowspan > 1) {
      	                     tableString += ' rowspan="' + columns[index][i].rowspan + '"';
      	                 }
      	                 if (typeof columns[index][i].colspan != 'undefined' && columns[index][i].colspan > 1) {
      	                     tableString += ' colspan="' + columns[index][i].colspan + '"';
      	                 }
      	                 if (typeof columns[index][i].field != 'undefined' && columns[index][i].field != '') {
      	                     nameList += ',{"f":"' + columns[index][i].field + '", "a":"' + columns[index][i].align + '"}';
      	                 }
      	                 tableString += '>' + columns[index][i].title + '</th>';
      	             }
      	         }
      	         tableString += '\n</tr>';
      	     });
      	 }
      	 // 载入内容
      	 var rows = printDatagrid.datagrid("getRows"); // 这段代码是获取当前页的所有行
      	 var nl = eval('([' + nameList.substring(1) + '])');
        nl.splice(3,1);
        nl.splice(4,5);
        nl.splice(3,0,nl[4]);
        nl.splice(4,0,nl[6]);
        nl.splice(6,2);
      	 for (var i = 0; i < rows.length; ++i) {
      	     tableString += '\n<tr>';
      	     $(nl).each(function (j) {
      	         var e = nl[j].f.lastIndexOf('_0');
                 var k = nl[j].f.lastIndexOf('.');
      	
      	         tableString += '\n<td';
      	         if (nl[j].a != 'undefined' && nl[j].a != '') {
      	             tableString += ' style="text-align:' + nl[j].a + ';style="WORD-WRAP: break-word" width="20"';
      	         }else{
      	        	 tableString += ' "TABLE-LAYOUT:fixed;WORD-BREAK:break-all" width="20"';
      	         }
      	         tableString += '>';
      	         if (e + 2 == nl[j].f.length) {
      	             tableString += rows[i][nl[j].f.substring(0, e)];
      	         }else if(k>-1){
                     var childs=nl[j].f.split('.');
                     tableString+=rows[i][childs[0]][childs[1]];
                 }
      	         else{
      	        	 if(typeof(rows[i][nl[j].f])!='undefined'&&rows[i][nl[j].f]!=null){
      	        		 tableString += rows[i][nl[j].f];
      	        	 }
      	        	 if(j==0){
      	        		tableString +=(i+1);
      	        	 }
      	         }
      	         tableString += '</td>';
      	     });
      	     tableString += '\n</tr>';
      	 }
      	 tableString += '\n</table>';
      	  $("#printdata").val(tableString);
      	 var popupwin =window.open("print", '_blank');
      }

    //明细
    function _moreList(index,type){
        $('#detailModal').modal('show');
        $('#detailModal').on('shown.bs.modal', function () {
            $("#detailList").empty();
            _searchDetailList(index,type);
        });
    }

    function _searchDetailList(index,type){
        var rows = $('#vMoneyTable').datagrid('getData').rows[index];
        $("#impleProjectName").val(rows.impleProjName);
        $("#orgName").val(rows.developOrgName);
        $("#openDay").val(rows.startDt);
        $("#finishDay").val(rows.endDt);
        var list;
        if(type=="totalIssued"){
            list = rows.totalIssuedFunds;
        }else if(type=="totalInvest"){
            list = rows.totalInvestFunds;
        }else{
            list = rows.totalFundFunds;
        }
        // for(var i =0;i<list.length;i++){
        //     list[i].endDt = rows.endDt;
        //     list[i].startDt = rows.startDt;
        //     list[i].impleProjName = rows.impleProjName;
        //     list[i].total = rows.total;
        //     list[i].buFundAll = rows.buFund;
        //     list[i].shengFundAll = rows.shengFund;
        //     list[i].gaoFundAll = rows.gaoFund;
        //     list[i].shiFundAll = rows.shiFund;
        //     list[i].qiFundAll = rows.qiFund;
        // }
        var datas = {
            rows: list,
            order:'asc',
            pageNumber:1,
            pageSize:200,
            sort:'',
            total:200,
            totalPages:1,
        }
        var columns=[[
            {field:'year',title:"年份",width:100,fitColumns:true},
            {field:'totalFund',title:"合计",width:100,fitColumns:true},
            {field:'buFund',title:"部补助",width:100,fitColumns:true},
            {field:'shengFund',title:"省财政",width:100,fitColumns:true},
            {field:'gaoFund',title:"政府融资",width:100,fitColumns:true},
            {field:'shiFund',title:"市州配套",width:100,fitColumns:true},
            {field:'qiFund',title:"企业自筹",width:100,fitColumns:true},
            {field:'otherFund',title:"其它",width:100,fitColumns:true},
        ]];
        $('#detailList').datagrid({
            method:'post',
            dataType : 'json',
            queryParams:null,
            nowrap:false,
            fitColumns:true,
            columns:columns,
            data:datas,
            loadMsg:'数据加载中，请耐心等待',
            onBeforeLoad : function(param){
                for (var key in param) {
                    if(key=="rows"){
                        delete param[key];
                    }
                }
            }
        });
    }

    return {
        init : _init,
        moreList:_moreList
    }
}();